/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { HarSigningOptions } from '../../../builder/inner-java-command-builder/har-signing-options.js';
import { NewSigningOptions } from '../../../builder/inner-java-command-builder/new-signing-options.js';
import { SigningOptions } from '../../../builder/inner-java-command-builder/signing-options.js';
import { SignTypeEnum } from '../../../enum/sign-type-enum.js';
import { ProjectModel } from '../../../model/project/project-model.js';
import { ProjectBuildProfile } from '../../../options/build/project-build-profile.js';
import { SdkInfo } from '../../../sdk/sdk-info.js';

import { BinSignCommandBuilder } from './bin-sign-command-builder.js';
import { CommonSignCommandBuilder } from './common-sign-command-builder.js';
import { HapSignCommandBuilder } from './hap-sign-command-builder';
import { HapV2SignCommandBuild } from './hapv2-sign-command-build.js';
import { HarSignCommandBuilder } from './har-sign-command-build.js';
import { SignModel } from './sign-model.js';

/**
 * 根据工程api 状态创建不同的签名配置
 *
 * @since 2022/03/01
 */
export class SignCommandFactory {
  private readonly _compileSdkVersion: number;

  constructor(compileSdkVersion: number) {
    this._compileSdkVersion = compileSdkVersion;
  }

  createCommandBuilder(
    projectModel: ProjectModel,
    signingConfig: ProjectBuildProfile.SigningConfigBuildOpt,
    sdkInfo: SdkInfo,
    signModel: SignModel,
  ): CommonSignCommandBuilder {
    switch (signModel.getSignType()) {
      case SignTypeEnum.HOS_HAP:
        // sign hap which contains shell
        return this._compileSdkVersion > 8
          ? new HapSignCommandBuilder(projectModel, signingConfig, sdkInfo, signModel, new NewSigningOptions())
          : new HapV2SignCommandBuild(projectModel, signingConfig, sdkInfo, signModel, new SigningOptions());
      case SignTypeEnum.APP:
        // only HarmonyOS or OpenHarmony
        return signingConfig.type === 'HarmonyOS' && this._compileSdkVersion < 9
          ? new HapV2SignCommandBuild(projectModel, signingConfig, sdkInfo, signModel, new SigningOptions())
          : new HapSignCommandBuilder(projectModel, signingConfig, sdkInfo, signModel, new NewSigningOptions());
      case SignTypeEnum.BIN:
        return new BinSignCommandBuilder(projectModel, signingConfig, sdkInfo, signModel, new NewSigningOptions());

      case SignTypeEnum.HAR:
        return new HarSignCommandBuilder(projectModel, signingConfig, sdkInfo, signModel, new HarSigningOptions());
      default:
        break;
    }

    return this._compileSdkVersion < 8
      ? new HapV2SignCommandBuild(projectModel, signingConfig, sdkInfo, signModel, new SigningOptions())
      : new HapSignCommandBuilder(projectModel, signingConfig, sdkInfo, signModel, new NewSigningOptions());
  }
}
